package com.yuanli.cosmeticmirror.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseFragment;
import com.yuanli.cosmeticmirror.bean.TutorialBean;
import com.yuanli.cosmeticmirror.bean.TutorialTypeBean;
import com.yuanli.cosmeticmirror.model.iview.ITutorialView;
import com.yuanli.cosmeticmirror.persenter.TutorialPersenter;
import com.yuanli.cosmeticmirror.view.activity.VideoPlayActivity;
import com.yuanli.cosmeticmirror.view.adapter.TutorialAdapter;

import butterknife.BindView;

public class TutorialFragment extends BaseFragment implements ITutorialView {

    @BindView(R.id.tutorial_rv)
    RecyclerView tutorialRv;
    private TutorialAdapter adapter;
    private TutorialPersenter persenter;
    private int id;
    private int index;

    public TutorialFragment(int i, int id) {
        this.index = i;
        this.id = id;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_tutorial;
    }

    @Override
    protected void init(Bundle paramBundle) {
        persenter = new TutorialPersenter(this, this);
    }

    @Override
    protected void initView(View view) {
        adapter = new TutorialAdapter();
        tutorialRv.setLayoutManager(new GridLayoutManager(requireActivity(), 2));
        tutorialRv.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view1, position) -> {
            TutorialBean.DataBean dataBean = (TutorialBean.DataBean) adapter.getItem(position);
            if (dataBean != null) {
                startActivity(new Intent(requireActivity(), VideoPlayActivity.class).putExtra("url", dataBean));
            }
        });
    }

    @Override
    protected void stopLoadData() {
    }

    @Override
    protected void startLoadData() {
        showLoadDialog();
        persenter.getTutorial(String.valueOf(id));
    }

    @Override
    public void onSucess(TutorialBean bean) {
        dialogDismiss();
        adapter.setNewInstance(bean.getData());
    }

    @Override
    public void error() {
        dialogDismiss();
        showToast("获取教程失败");
    }

    @Override
    public void onTypeSuccess(TutorialTypeBean object) {
    }
}
